package com.tan.androidtest.java;

import android.support.annotation.NonNull;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by Administrator on 2017/11/21.
 */

public class TestInstanceCompare {


    public static void main(String[] args) {
        System.out.println("===============");
        List<Man> list = new ArrayList<>();
        list.add(new TestInstanceCompare().new Man("e", 1));
        list.add(new TestInstanceCompare().new Man("a", 5));
        list.add(new TestInstanceCompare().new Man("b", 2));
        list.add(new TestInstanceCompare().new Man("d", 1));
        list.add(new TestInstanceCompare().new Man("百度", 4));
       /* Collections.sort(list, new Comparator<Man>() {
            @Override
            public int compare(Man o1, Man o2) {
                return o1.name.compareTo(o2.name);
            }
        });*/
        Collections.sort(list);
        System.out.println(list);



    }

    class Man implements Comparable<Man> {
        public Man(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String name;
        public int age;

        @Override
        public String toString() {
            return "Man: [ name = " + name + " age = " + age + " ]";
        }

        @Override
        public int compareTo(@NonNull Man o) {
            return this.name.compareTo(o.name);
        }
    }

}
